<?php

namespace App\Admin\Controllers\User;


use App\Models\User\LoginLog;
use Dcat\Admin\Admin;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Repositories\EloquentRepository;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Widgets\Tab;

class LogController extends AdminController
{
    /**
     * @var $repository EloquentRepository
     */
    protected $repository;

    public function __construct()
    {
        Admin::translator()->setPath("user");
    }

    public function types($id,$type,Content $content)
    {
        $types = ['login','other'];
        if(!in_array($type,$types)){
            throw new \Exception("不支持的日志类型！");
        }
        $tab = Tab::make();
        foreach ($types as  $v) {
            $name = admin_trans_field(sprintf('%s-log',$v));
            $type == $v ? $tab->add($name,$this->{$v}($id),true) :  $tab->addLink($name,admin_url(sprintf("user/%d/log/%s",$id,$v)));
        }
        return $content
            ->translation($this->translation())
            ->title($this->title())
            ->description($this->description()['index'] ?? trans('admin.list'))
            ->body($tab->withCard());
    }

    /**
     * 登录日志列表
     * @param $uid
     * @return Grid
     */
    protected function login($uid)
    {
        $model = LoginLog::where("uid",$uid)->orderBy("id","desc");
        $repository = new EloquentRepository($model);
        return Grid::make($repository , function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('login_from');
            $grid->column('login_result');
            $grid->column('login_message');
            $grid->column('login_ip')->display(function ($value){
                return long2ip($value);
            });
            $grid->column('os');
            $grid->column('created_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
            $grid->disableCreateButton();
            $grid->disableEditButton();
            $grid->disableDeleteButton();
            $grid->disableActions();
            $grid->disableFilterButton();
        });
    }

    /**
     * 其他日誌
     * @param $uid
     * @return Grid
     */
    protected function other($uid)
    {
        $model = LoginLog::where("uid",$uid)->orderBy("id","desc");
        $repository = new EloquentRepository($model);
        return Grid::make($repository , function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('login_from');
            $grid->column('login_result');
            $grid->column('login_message');
            $grid->column('login_ip')->display(function ($value){
                return long2ip($value);
            });
            $grid->column('os');
            $grid->column('created_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
            $grid->disableCreateButton();
            $grid->disableEditButton();
            $grid->disableDeleteButton();
            $grid->disableActions();
            $grid->disableFilterButton();
        });
    }
}
